#include "game_version.h"
#include "game_application.h"
#include "game_event.h"
#include "game_def.h"
#include "game_macros.h"
#include "game_log.h"
#include <unistd.h>



class CCEventDispatcher:public CGameEventDispatcher,public CGameObject{
private:
  UINT32 _count;
public:
  CCEventDispatcher():_count(0){};
  virtual BOOL init();

  BOOL onBeat(CGameEvent *event);
  
};

BOOL CCEventDispatcher::init(){

  do{
    CC_BREAK_IF(!CGameEventDispatcher::init());
    
    addEventListener(CGameEvent::CONNECT, CGameEventHandler(this, &CCEventDispatcher::onBeat));


    return TRUE;
  }while(true);



  return FALSE;
}

class CCEvent:public CGameEvent{
private:
  int _data;
public:
  CCEvent(const char* type, CGameObject* currentTarget,int data):_data(data),CGameEvent(type,currentTarget){}
  int getData() const{
    return _data;
  }
};

BOOL CCEventDispatcher::onBeat(CGameEvent *event)
{
  // logger->info("%s", "onConnect");
  // _beat_timer.oneshot(0, EvTimerHandler(this, &Application::onBeat));
  
  // C_WARN("%s","CCEventDispatcher::onBeat");
  // C_ERROR("%s","CCEventDispatcher::onBeat");
  // C_LOG("%s","CCEventDispatcher::onBeat");

  CCEvent* evt = static_cast<CCEvent*>(event);


  CC_LOG("CCEventDispatcher","beat with data(%d)",evt->getData());

  _count++;

  if(_count >= 10){
    removeEventListener(CGameEvent::CONNECT, CGameEventHandler(this, &CCEventDispatcher::onBeat));
  }

  

  return TRUE;
}




class Logger{

};

int main(){
  
  CCEventDispatcher dispatcher;
  CGameObject obj;
  int idx = 0;
  if(!dispatcher.init())
    return -1;
  do{

    // C_LOG("%s","tick");
    
    

    

    event_base_loop(g_CGameEV.getBase(), EVLOOP_NONBLOCK);

    // if((idx & 3) == 0 ){
      C_LOG("tick %d",idx);
      CGameEvent *evt = new CCEvent(CGameEvent::CONNECT,&obj,idx);
      dispatcher.dispatch(evt);
    // }
    
    idx++;
    usleep(1000 * 1000);
  }while(true);

  return -1;
}
